<?php

declare (strict_types=1);

namespace app\bbxw\controller;


use think\admin\Controller;
use think\admin\helper\QueryHelper;
use app\bbxw\model\Customer as CustomerMod;
use app\bbxw\model\CustomerAmount;

/**
 * 客户管理
 * @class Package
 * @package app\bbxw\controller
 */
class Customer extends Controller
{

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'customer';
    /**
     * 客户管理
     * @auth true
     * @menu true
     * @login true
     */
    public function index()
    {
        CustomerMod::mQuery()->layTable(function () {
            $this->title = '客户管理';
        }, function (QueryHelper $query) {
            $query->like('name,nickname')->order('id asc');
        });
    }

    /**
     * 图文列表数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _index_page_filter(array &$data)
    {

        foreach ($data as &$v){
            $prestore_amount  = $v['prestore_amount'];
            if(empty($v['prestore_amount'])){
                $prestore_amount = "0";
            }
            $use_amount  = $v['use_amount'];
            if(empty($v['prestore_amount'])){
                $use_amount = "0";
            }
            $v['balance'] = bcsub($prestore_amount,$use_amount,2);
            $v['grade_name'] = "普通";
            if($v['grade']){
                $v['grade_name'] = $this->app->db->name("membership_card")->where('id',$v['grade'])->value('title');
            }
            $points  = $v['points'];
            if(empty($v['points'])){
                $points = "0";
            }
            $use_points  = $v['use_points'];
            if(empty($v['use_points'])){
                $use_points = "0";
            }
            $v['points_balance'] = bcsub($points,$use_points,2);

        }
    }


    /**
     * 添加客户
     * @auth true
     */
    public function add()
    {
        CustomerMod::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()) {

        } else {
            $this->membership_card = $this->app->db->name("membership_card")->select()->toArray();
        }

    }
    /**
     * 编辑客户
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        CustomerMod::mForm('form');
    }


    /**
     * 删除客户
     * auth true
     */
    public function remove()
    {
        CustomerMod::mDelete();
    }
    protected function _form_result(bool $result, array $data)
    {
        if ($result) {
            $this->app->db->name("play_accompany")
                ->where(['customer_id'=>$data['id']])->update(['customer_name'=>$data['name'],'nickname'=>$data['nickname']]);
            $this->app->db->name("play_accompany")->where(['customer_name'=>$data['name']])->update(['customer_id'=>$data['id']]);
            $this->success('数据保存成功！');
        }else{
            $this->error('数据保存失败！');
        }
    }


    /**
     * 重置密码
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function resetPassword()
    {
        $id = $this->app->request->get('id');
        $this->app->db->name($this->table)->where(['id'=>$id])->update(['password'=>md5('123123')]);
        $this->success('重置密码成功,新的密码为123123');
    }

    public function customerAmount(){

        CustomerAmount::mQuery()->layTable(function () {
            $this->customer_id = input('id');
        }, function (QueryHelper $query) {

            $query->equal('customer_id')->order('id desc');
        });

    }
    /**
     * 修改用户密码
     * @auth true
     */
    public function pass()
    {
        if ($this->request->isGet()) {

            CustomerMod::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
                'password.require'            => '登录密码不能为空！',
                'repassword.require'          => '重复密码不能为空！',
                'repassword.confirm:password' => '两次输入的密码不一致！',
            ]);
            $user = CustomerMod::mk()->findOrEmpty($data['id']);
            if ($user->isExists() && $user->save(['password' => md5($data['password'])])) {

                $this->success('密码修改成功，请使用新密码登录！', '');
            } else {
                $this->error('密码修改失败，请稍候再试！');
            }
        }
    }

    public function resetCustomer()
    {
        $this->_queue("重新计算金额",  "xbbxw:resetCustomer");

    }

}